package com.starblink.brand.adapter

import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.enableCorner
import com.starblink.android.basic.extension.dp2px
import com.starblink.brand.bean.FilterPriceInputBean
import com.starblink.brand.databinding.CellFilterPriceBinding
import com.starblink.brand.ui.FilterPriceI

class FilterInputCell (binding: CellFilterPriceBinding,var filterDialogI:FilterPriceI?=null) :
    BaseBindingVH<FilterPriceInputBean, CellFilterPriceBinding>(binding) {

    private var minCall: ((number: Double?) -> Unit)? = null
    private var maxCall: ((number: Double?) -> Unit)? = null

    init {
        binding.inputMax.enableCorner(8.dp2px())
        binding.inputMin.enableCorner(8.dp2px())
        binding.inputMin.addTextChangedListener {
            it?.toString()?.run {
                minCall?.invoke(
                    try {
                        toDouble()
                    } catch (e: Exception) {
                        e.printStackTrace()
                        null
                    }
                )
            }
            filterDialogI?.changeBtnStates()
        }
        binding.inputMax.addTextChangedListener {
            it?.toString()?.run {
                maxCall?.invoke(
                    try {
                        toDouble()
                    } catch (e: Exception) {
                        e.printStackTrace()
                        null
                    }
                )
            }
            filterDialogI?.changeBtnStates()
        }
        binding.inputMin.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                v.clearFocus()
            }
            false
        }
        binding.inputMax.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                v.clearFocus()
            }
            false
        }

    }

    override fun onBind(model: FilterPriceInputBean, position: Int) {
        if(model.min!=null){
            binding.inputMin.setText(model.min?.toInt().toString())
        }else{
            binding.inputMin.setText("")
        }
        if(model.max!=null){
            binding.inputMax.setText(model.max?.toInt().toString())
        }else{
            binding.inputMax.setText("")
        }
        minCall = {
            model.min = it
        }
        maxCall = {
            model.max = it
        }
    }

}